package test11;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Award2 implements Runnable {

    private static int[] arr = {10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300, 700};
    ArrayList<Integer> list = new ArrayList<>();
    Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true) {
            lock.lock();
            //获取随机下标：
            int i = new Random().nextInt(arr.length);
            if (!list.contains(i)) {
                list.add(i);

                try {
                    Thread.sleep(200);

                    System.out.println(Thread.currentThread().getName() +
                            "产生了一个" + arr[i] + "元大奖");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            lock.unlock();

            //集合list存满了则break;
            if (list.size() == arr.length) {
                break;
            }
        }
    }
}
